package cn.bluemobi.dylan.step.ui.my.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.moder.bean.Goods;
import cn.bluemobi.dylan.step.moder.bean.Score;

public class GoodsAdapter extends RecyclerView.Adapter {
    private List<Goods> list;
    private Context context;

    public GoodsAdapter(Context context, List<Goods> list) {
        this.context = context;
        this.list = list;
    }

    public void notifyChanged(List<Goods> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private OnItemOnClicklisten onItemOnClicklisten;

    public void setOnItemOnClilcklisten(OnItemOnClicklisten onItemOnClicklisten) {
        this.onItemOnClicklisten = onItemOnClicklisten;
    }

    public interface OnItemOnClicklisten {
        void onItemClick(View v, int position);
        void onBuyGoodsClick(View v, int position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods, parent,false);
        return new BleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BleViewHolder mHolder = (BleViewHolder) holder;

        mHolder.tv_name.setText(list.get(position).goodsName);
        mHolder.tv_score.setText("所需积分："+list.get(position).goodsScore+"积分");

        mHolder.img_buy_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemOnClicklisten != null) {
                    onItemOnClicklisten.onBuyGoodsClick(view, position);
                }
            }
        });

        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemOnClicklisten != null) {
                    onItemOnClicklisten.onItemClick(v, position);
                }
            }
        });


    }


    class BleViewHolder extends RecyclerView.ViewHolder {


        public TextView tv_name;
        public TextView tv_score;
        public ImageView img_buy_goods;

        public BleViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_score = view.findViewById(R.id.tv_score);
            img_buy_goods = view.findViewById(R.id.img_buy_goods);

        }
    }
}
